package info.peigen.hotpot.manage.core.handler;

import info.peigen.hotpot.common.core.result.ResultBase;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Filter;
import org.noear.solon.core.handle.FilterChain;
import org.noear.solon.validation.ValidatorException;

/**
 * @author noear 2022/10/4 created
 */
@Slf4j
@Component
public class GlobalErrorFilter implements Filter {
    @Override
    public void doFilter(Context ctx, FilterChain chain) throws Throwable {
        try {
            chain.doFilter(ctx);
            // if (!ctx.getHandled()) {
            //     ctx.status(404);
            // }
        } catch (ValidatorException e) {
            //验证器异常
            ctx.render(ResultBase.failDetail(e.getMessage()));
        } catch (Throwable e) {
            e.printStackTrace();
            ctx.render(ResultBase.failDetail(e.getMessage()));
        }
    }
}
